package com.jsmy.marketing.activity;

import android.os.Build;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.jsmy.marketing.R;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.base.BaseActivity;
import com.jsmy.marketing.bean.BaseBean;
import com.jsmy.marketing.bean.PlanInfoBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.manager.UserManager;
import com.jsmy.marketing.utils.MesssageUtil;
import com.jsmy.marketing.utils.ShareUtil;
import com.jsmy.marketing.utils.ToastUtil;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PlanDetailActivity extends BaseActivity implements UMShareListener {

    public String planid;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    PlanInfoBean.DataBean bean;

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.m_webview)
    WebView mWebview;

    String sharePt;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.action_setting1)
    TextView actionSetting1;


    @Override
    protected int resId() {
        return R.layout.activity_plan_detail;
    }

    @Override
    protected void initData() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        planid=getPrevExtras().getString("planid");
        requestData();
    }

    private void requestData() {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", UserManager.getInstance().getId());
        map.put("planid", planid);
        mPresenter.loadData(RetrofitManager.getInstance().getgtjhinfo(map),Link.APP_GTJH_INFO);
    }

    @OnClick({R.id.action_setting1, R.id.action_setting2, R.id.action_setting3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.action_setting1:
                if (bean.getIsok().equals("Y")){
                    ToastUtil.showShort(this,"计划已完成！");
                }else {
                    saverwflaginfo(planid,"Y");
                }
                break;
            case R.id.action_setting2:
                MesssageUtil.call(this, bean.getDh());
                break;
            case R.id.action_setting3:
                ShareUtil.shareWeb(this, bean.getUrl(), bean.getGtlx(), bean.getNr(), this);
                break;
        }
    }

    private void saverwflaginfo(String plantId, String y) {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("planid", plantId);
        map.put("flat", y);
        mPresenter.loadData(RetrofitManager.getInstance().saverwflaginfo(map), Link.APP_SAVE_RW_FLAG_INFO);
    }

    @Override
    public void refreshData(Object object, String api_id) {
        switch (api_id) {
            case Link.APP_GTJH_INFO:
                PlanInfoBean planInfoBean= (PlanInfoBean) object;
                if (planInfoBean.getCode().equals("Y")){
                    bean = ((PlanInfoBean) object).getData();
                    setPlanInfo();
                }else {
                 ToastUtil.showShort(this,planInfoBean.getMsg());
                }
                break;
            case Link.APP_SAVE_RW_FLAG_INFO:
                BaseBean baseBean= (BaseBean) object;
                if (baseBean.getCode().equals("Y")){
                    ToastUtil.showShort(this,"成功");
                     finish();
                }else {
                    ToastUtil.showShort(this,baseBean.getMsg());
                }
                break;
        }
    }
    private void setPlanInfo() {
        tvName.setText("客户名称：" + bean.getXm());
        tvTitle.setText("计划标题：" + bean.getGtlx());
        tvTime.setText("计划时间：" + bean.getSj());
        tvContent.setText("计划内容：" + bean.getNr());
        tvText.setText("" + bean.getUrl());
        if (bean.getIsok().equals("Y")){
            actionSetting1.setText("已完成");
        }
        mWebview.setVerticalScrollbarOverlay(true); //指定的垂直滚动条有叠加样式
        WebSettings settings = mWebview.getSettings();
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放
        settings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebview.clearHistory();
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                return super.shouldOverrideUrlLoading(view, request);
            }

//            @Override
//            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                    handler.proceed();
////                super.onReceivedSslError(view, handler, error);
//            }

        });
        mWebview.loadUrl("" + bean.getUrl());
    }
    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                sharePt = "QQ";
                break;
            case QZONE:
                sharePt = "QZONE";
                break;
            case SINA:
                sharePt = "SINA";
                break;
            case WEIXIN:
                sharePt = "WEIXIN";
                break;
            case WEIXIN_CIRCLE:
                sharePt = "WEIXIN_CIRCLE";
                break;
            case SMS:
                sharePt = "SMS";
                break;
            case EMAIL:
                sharePt = "EMAIL";
                break;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("id", bean.getZlid());
        map.put("planid", planid);
        map.put("pt", sharePt);
        map.put("obj", "");
        mPresenter.loadData(RetrofitManager.getInstance().saveFxInfo(map), Link.APP_SAVE_FX_INFO);
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.showShort(this, "分享失败，" + throwable.getMessage());
    } @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.showShort(this, "分享取消");
    }


}
